﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;

namespace Schism.libs
{
    public interface IAssignable<T, K>
    {
        void Save(string filename);
        void Load(string filename);

        /// <summary>
        /// Extracts data From the Main Object, assignes it to this object
        /// </summary>
        void ExtractFrom(T obj);
        
        /// <summary>
        /// Assigns from a Object loaded in from the IOhandler to this object
        /// </summary>
        void AssignToo(K obj);

        /// <summary>
        /// Assigns all extracted to the T class
        /// </summary>
        void Assign(T obj);
    }


    public static class IOHandle
    {
        public static T LoadFrom<T>(string filename)
        {
            using (FileStream fs = File.OpenRead(string.Format(@"{0}\data\{1}", ObjectHolder.GameObject.Content.RootDirectory, filename)))
            {
                BinaryFormatter binFormat = new BinaryFormatter();
                return (T)binFormat.Deserialize(fs);
            }
        }
        public static void Save<T>(string filename, T graph)
        {
            using (FileStream fs = File.Create(string.Format(@"{0}\data\{1}", ObjectHolder.GameObject.Content.RootDirectory, filename)))
            {
                BinaryFormatter binFormat = new BinaryFormatter();
                binFormat.Serialize(fs, graph);
            }
        }
    }
}
